package pzl.com.bluetooth.utils;

import android.content.Context;
import android.os.SystemClock;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;

/**
 * @author zl.peng
 * @version [1.0, 2016-10-11]
 */
public class Utils {
    public static Toast toast;
    private static final String DEFAULT_ENCODING = "GBK";//编码
    private static final int PROTECTED_LENGTH = 51200;// 输入流保护 50KB

    public static String readStream(InputStream input) throws Exception {
        if (input == null) {
            throw new Exception("输入流为null");
        }
        //字节数组
        byte[] bcache = new byte[2048];
        int readSize = 0;//每次读取的字节长度
        int totalSize = 0;//总字节长度
        ByteArrayOutputStream infoStream = new ByteArrayOutputStream();
        try {
            //一次性读取2048字节
            while ((readSize = input.read(bcache)) > 0) {
                totalSize += readSize;
                if (totalSize > PROTECTED_LENGTH) {
                    throw new Exception("输入流超出50K大小限制");
                }
                //将bcache中读取的input数据写入infoStream
                infoStream.write(bcache, 0, readSize);
            }
        } catch (IOException e1) {
            throw new Exception("输入流读取异常");
        } finally {
            try {
                //输入流关闭
                input.close();
            } catch (IOException e) {
                throw new Exception("输入流关闭异常");
            }
        }

        try {
            return infoStream.toString(DEFAULT_ENCODING);
        } catch (UnsupportedEncodingException e) {
            throw new Exception("输出异常");
        }
    }

    public static void show(Context context, String msg) {
        String stamp = Long.toString(SystemClock.currentThreadTimeMillis());
        if (toast == null) {
            toast = Toast.makeText(context, msg + "-" + stamp.substring(stamp.length() - 2, stamp.length()), Toast.LENGTH_LONG);
        } else {
            toast.setText(msg + "-" + stamp.substring(stamp.length() - 2, stamp.length()));
        }
        toast.show();
    }


}
